package com.pingancar.creditmanage.action.auth;

import com.pingancar.creditmanage.action.AdminAction;
import com.pingancar.creditmanage.action.ShopAction;
import com.pingancar.creditmanage.action.UserAction;
import com.pingancar.creditmanage.pojo.AdminPojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.pojo.ShopuserPojo;
import com.pingancar.creditmanage.pojo.UserPojo;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by liymm on 2015-01-28.
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (!handler.getClass().isAssignableFrom(HandlerMethod.class))
            return true;

        UserAuthPassport uAuth = ((HandlerMethod) handler).getMethodAnnotation(UserAuthPassport.class);

        if (uAuth == null) {
            uAuth = ((HandlerMethod)handler).getBeanType().getAnnotation(UserAuthPassport.class);
        }

        if (uAuth != null && uAuth.validate())
            return validateUserAuthority(request, response);

        ShopAuthPassport sAuth = ((HandlerMethod) handler).getMethodAnnotation(ShopAuthPassport.class);

        if (sAuth == null) {
            sAuth = ((HandlerMethod)handler).getBeanType().getAnnotation(ShopAuthPassport.class);
        }

        if (sAuth != null && sAuth.validate())
            return validateShopAuthority(request, response);

        AdminAuthPassport aAuth = ((HandlerMethod) handler).getMethodAnnotation(AdminAuthPassport.class);

        if (aAuth == null) {
            aAuth = ((HandlerMethod)handler).getBeanType().getAnnotation(AdminAuthPassport.class);
        }

        if (aAuth != null && aAuth.validate())
            return validateAdminAuthority(request, response);

        return true;
    }

    boolean validateUserAuthority(HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserPojo userPojo = (UserPojo)request.getSession().getAttribute("user");
        if (userPojo != null)
            return true;

        jumpPage(request, response, UserAction.LOGIN_ACTION);

        return false;
    }

    boolean validateShopAuthority(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)request.getSession().getAttribute("shop");
        if (shopinfoPojo != null)
            return true;

        jumpPage(request, response, ShopAction.DEFAULT_ACTION);

        return false;
    }

    boolean validateAdminAuthority(HttpServletRequest request, HttpServletResponse response) throws Exception {
        AdminPojo adminPojo = (AdminPojo)request.getSession().getAttribute("admin");
        if (adminPojo != null)
            return true;

        jumpPage(request, response, AdminAction.DEFAULT_ACTION);

        return false;
    }

    void jumpPage(HttpServletRequest request, HttpServletResponse response, String relativeURL) throws Exception {
        String url = request.getSession().getServletContext().getContextPath() + relativeURL;
        response.sendRedirect(url);
    }
}
